class Utils {
	
	constructor(){
		this.baseUrl = 'https://test.asiatop.net/api-vouchery-app/api/v1'
	}
	
	getUserInfo(){
		// 调用登录的api
		uni.login({
			// success:function(res) {
			success:res =>{
				console.log(res);
				this.request({
					url:'/auth/wechat',
					data:{
						code:res.code,
						captcha: "940932",
						countryCode: "86",
						merchantId:'',
						password:'',
						phone: 15170734385
					},
					success: res =>{
						console.log(res,'res');
					}
				})
			}
		})
	}
	request(option = {
		showLoading:false
	}) {
		// 判断是否存URL
		if(!option.url){
			return false
		}
		if(option.showLoading){
			this.showLoading()
		}
		
		uni.request({
			url:this.baseUrl + option.url,
			data:option.data ?option.data:{},
			header:option.header ? option.header : {},
			method:option.method ? option.method : 'GET',
			success(response) {
				uni.hideLoading()
				// 后端数据异常
				if(response.data.code != 200) {
					// 将失败的结果传回去
					if(option.fail && typeof option.fail == 'function') {
						option.fail(response)
					}
				} else {
					// 将成功的结果返回
					// 将失败的结果传回去
					if(option.success && typeof option.success == 'function') {
						option.success(response.data)
					}
				}
			},
			fail:response => {
				console.log(response);
			}
		})
		
	}
	// 创建加载的loading效果
	showLoading() {
		const isShiwLoading = uni.getStorageInfoSync('isShiwLoading')
		if(isShiwLoading){
			uni.hideLoading()
			uni.setStorageSync('isShiwLoading',false)
		}
		
		uni.showLoading({
			title:'加载中...',
			complete:function (){
				uni.setStorageSync('isShiwLoading',true)
			},
			fail: function (){
				uni.setStorageSync('isShiwLoading',false)
			}
		})
	}
}

// 导出
export default new Utils()